<template>
  <div class="app-main-container">
    <QueryForm
      v-model="filters"
      :query-form-config="queryFormConfig"
      @handleQuery="getList(1)"
      @clear="clearList"
    />
    <el-table
      ref="table"
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      :header-row-class-name="'table-header'"
      class="doumee-element-table"
    >
      <el-table-column
        align="center"
        label="学员姓名"
        min-width="100"
        prop="name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="学员证件号"
        min-width="160"
        prop="idCard"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="性别"
        min-width="60"
        prop="sex"
      >
        <template v-slot="scope">
          <span v-if="scope.row.sex == '1'">男</span>
          <span v-if="scope.row.sex == '2'">女</span>
          <span v-if="scope.row.sex == '0'">保密</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="出生日期"
        prop="birthday"
        min-width="120"
      />
      <el-table-column
        align="center"
        label="会员信息"
        prop="memberInfo"
        min-width="250"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="创建时间"
        prop="createTime"
        min-width="160"
      />
      <el-table-column
        align="center"
        label="操作 "
        min-width="160"
        fixed="right"
      >
        <template slot-scope="scope">
          <div class="flex-cc">
            <el-button v-if="meta.indexOf('StudentAdd') > -1" type="text" @click="editStudent(scope.row)">编辑学员</el-button>
            <el-button v-if="meta.indexOf('courseListByStudent') > -1" type="text" @click="rowClickDetial(scope.row)">查看课程</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <Paginations
      :total="total"
      :page="pagination.page"
      @current-change="getList"
      @SizeChange="handleSizeChange"
    />

    <el-dialog
      title="编辑学员"
      :close-on-click-modal="modalDialog"
      :close-on-press-escape="modalDialog"
      :visible.sync="showStudent"
    >
      <el-form
        ref="formStudent"
        :model="formStudent"
        label-width="120px"
        :rules="ruleb"
        label-suffix="："
      >
        <el-form-item prop="name" label="学员姓名">
          <el-input
            v-model="formStudent.name"
            maxlength="20"
            placeholder="请输入学员姓名"
            clearable
          />
        </el-form-item>
        <el-form-item prop="sex" label="学员性别">
          <el-select
            v-model="formStudent.sex"
            style="width: 100%"
            placeholder="请选择性别"
          >
            <el-option value="1" label="男" />
            <el-option value="2" label="女" />
            <el-option value="0" label="保密" />
          </el-select>
        </el-form-item>
        <el-form-item prop="birthday" label="出生日期">
          <el-date-picker
            v-model="formStudent.birthday"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择出生日期"
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item prop="idCard" label="身份证号">
          <el-input
            v-model="formStudent.idCard2"
            maxlength="18"
            placeholder="请输入学员身份证号"
            clearable
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showStudent = false">取消</el-button>
        <el-button
          v-preventReClick
          :loading="studentLoading"
          type="primary"
          @click="onSubmitb"
        >确定</el-button>
      </div>
    </el-dialog>
    <Detail v-if="isShowDetail" ref="DetailRef" :meta="meta" @close="isShowDetail = false" />
  </div>
</template>

<script>
import { parseTime } from '@/utils/index'
import { studentManagers, kcStudentUpdate } from '@/api'
import { queryFormConfig } from './config'
import QueryForm from '@/components/QueryForm'
import Detail from './detail.vue'
export default {
  name: 'StudentAdmin',
  components: {
    QueryForm,
    Detail
  },
  data() {
    return {
      isShowDetail: false,
      modalDialog: this.$store.getters.dialog,
      queryFormConfig,
      meta: [],
      list: [],
      pagination: {
        page: 1,
        rows: 10
      },
      total: 0,
      filters: {},
      listLoading: true,

      showStudent: false,
      studentLoading: false,
      formStudent: {},
      ruleb: {
        name: [{ required: true, message: '请输入学员姓名', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.meta = this.$route.meta.buttons || []
    this.getList(1)
  },
  mounted() {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
  },
  activated() {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
  },
  methods: {
    handleSizeChange(value) {
      this.pagination.rows = value
      this.getList(1)
    },
    clearList() {
      this.filters = {}
      this.getList(1)
    },
    getList(page) {
      this.listLoading = true
      const { filters, pagination } = this
      const venueId = sessionStorage.getItem('venueId')
      if (page) {
        pagination.page = page
        this.pagination.page = page
      }
      pagination.firstQueryTime = parseTime(new Date())
      studentManagers({ pagination, param: { venueId, ...filters }})
        .then(res => {
          this.listLoading = false
          if (res.errorCode === '000000') {
            this.list = res.recordList
            this.total = res.totalCount
          }
        })
        .catch(() => {
          this.listLoading = false
        })
    },
    editStudent(row) {
      const obj = { ...row }
      if (obj.birthday) {
        obj.birthday = obj.birthday.slice(0, 10)
      }
      this.formStudent = obj
      this.showStudent = true
    },
    rowClickDetial(row) {
      this.isShowDetail = true
      this.$nextTick(() => {
        this.$refs.DetailRef.isShowDrawer = true
        this.$refs.DetailRef.detail = { ...row }
        this.$refs.DetailRef.getList()
      })
    },
    onSubmitb() {
      this.$refs.formStudent.validate((valid) => {
        if (valid) {
          const param = { ...this.formStudent }
          if (param.birthday) {
            param.birthday = param.birthday + ' 00:00:00'
          }
          if (param.idCard2) {
            param.idCard = param.idCard2
          }
          this.studentLoading = true
          kcStudentUpdate({
            param
          })
            .then((res) => {
              this.studentLoading = false
              if (res.errorCode === '000000') {
                this.$message.success('修改成功')
                this.showStudent = false
                this.getList()
              }
            })
            .catch((err) => {
              this.studentLoading = false
              console.log(err)
            })
        }
      })
    }
  }
}
</script>
